; AdvancedScriptEditor.au3

$debug = 0
$exe = """" & @ScriptDir & "\AdvancedScriptEditor_bin\FramelessScriptEditor.exe" & """"
$script = """" & @ScriptDir & "\AdvancedScriptEditor_bin\blankscript.txt" & """"

$path = $exe & " " & $script & " " & "800,600" & " " & "10,10"
If $debug = 1 Then MsgBox(0, "ASEDebug", "$path = " & $path)

Run($path)
WinWait("FramelessScriptEditor")
WinSetState("FramelessScriptEditor", "", @SW_MINIMIZE)

$ini = IniRead("AdvancedScriptEditor.ini", "3DRad", "Script", "ValueNotFound")
If $ini == "ValueNotFound" Then
	MsgBox(0, "Advanced Script Editor", "Value 'Script' not found in section '3DRad' in ini file 'AdvancedScriptEditor.ini'. Defaulting to 'v1.36'.")
	$scriptwindow = "Script v1.36"
Else
	$scriptwiindow = "Script " & $ini
EndIf

WinWaitActive("[CLASS:3DRADCLASS]")
$radrunning = 1
Do
	; If 3D Rad is closed, close FramelessScriptEditor.exe and AdvancedScriptEditor.exe
	If Not WinExists("[CLASS:3DRADCLASS]") Then
		$radrunning = 0
		WinClose("FramelessScriptEditor")
		Exit
	EndIf
	If WinExists($scriptwindow) Then
		If $debug = 1 Then MsgBox(0, "ASEDebug", "Starting script loop.")
		$doonce = 1
		$i = 0
		Do
			If $doonce = 1 Then
				If $debug = 1 Then MsgBox(0, "ASEDebug", "Starting $doonce loop.")
				$text = ControlGetText($scriptwindow, "", "[ID:0]")
				
				If $debug = 1 Then MsgBox(0, "ASEDebug", "Writing text to file: " & $text)
				$filehandle = FileOpen(@ScriptDir & "\AdvancedScriptEditor_bin\blankscript.txt", 2)
				If $filehandle = -1 Then MsgBox(0, "Error", "Unable to open file.")
				FileWrite($filehandle, $text)
				FileClose($filehandle)
				
				;If $debug = 1 Then MsgBox(0, "ASEDebug", "Reloading script file in FSE.")
				;ControlClick("FramelessScriptEditor", "", "[NAME:btnReloadFromFile]")
				;ControlSetText("FramelessScriptEditor", "", 2819584, $text);[CLASS:WindowsForms10.Scintilla.app.0.bf7771; INSTANCE:1]
				
				WinSetState("FramelessScriptEditor", "", @SW_RESTORE)
				WinActivate($scriptwindow)
				$doonce = 0
			EndIf
			
			; This makes sure that if the Script window is active, FramelessScriptEditor is displayed above it.
			If WinActive($scriptwindow) Then
				WinSetOnTop("FramelessScriptEditor", "", 1)
			Else
				WinSetOnTop("FramelessScriptEditor", "", 0)
			EndIf
			
			; This minimises FramelessScriptEditor and exits the 'Do Until' loop when the Script window is closed.
			If Not WinExists($scriptwindow) Then
				WinSetState("FramelessScriptEditor", "", @SW_MINIMIZE)
				$i = 10
			EndIf
			
			; Each statement below has it own 'If Then' loop - this protects against errors if the Script window is closed after the 'If Then' loop has started, but before it finishes.
			If WinActive($scriptwindow) Then $currentSizePos = WinGetPos($scriptwindow)
			If WinActive($scriptwindow) Then $x = $currentSizePos[0] + 332
			If WinActive($scriptwindow) Then $y = $currentSizePos[1] + 33
			If WinActive($scriptwindow) Then $currentSizeFS = WinGetClientSize($scriptwindow)
			If WinActive($scriptwindow) Then $width = $currentSizeFS[0] - 328
			If WinActive($scriptwindow) Then $height = $currentSizeFS[1] - 5
			If WinActive($scriptwindow) Then WinMove("FramelessScriptEditor", "", $x, $y, $width, $height)
			
			If $debug = 1 Then MsgBox(0, "ASEDebug", "Reading text from file.")
			$newfilehandle = FileOpen(@ScriptDir & "\AdvancedScriptEditor_bin\blankscript.txt", 0)
			$newtext = FileRead($newfilehandle)
			FileClose($newfilehandle)
			If $debug = 1 Then MsgBox(0, "ASEDebug", "Comparing $newtext: " & $newtext & " and $text: " & $text)
			If $newtext == $text Then
				If $debug = 1 Then MsgBox(0, "ASEDebug", "No change.")
				$placeholder = 5
			Else
				If $debug = 1 Then MsgBox(0, "ASEDebug", "Change! Text differs from original!")
				WinActivate($scriptwindow)
				ControlSetText($scriptwindow, "", "[ID:0]", $newtext)
				$text = $newtext
				If $debug = 1 Then MsgBox(0, "ASEDebug", "$text now equals: " & $text)
				WinActivate("FramelessScriptEditor")
			EndIf
		Until $i = 10
	EndIf
Until $radrunning = 0